﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    internal static class SerializedFileContainerWriter
    {
        private static StringBuilder GetArrayOfByteArrays(SerializedFile file)
        {
            StringBuilder builder = new StringBuilder(string.Empty);
            builder.Append("new " + GetTypeNameForFile(file.Name) + "() as ByteArray");
            return builder;
        }

        private static string GetConstructor(IEnumerable<SerializedFile> files)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("     \r\n       public function ProjectSerializedFileContainer()\r\n       {\r\n              files = new Dictionary();\r\n");
            IEnumerator<SerializedFile> enumerator = files.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    SerializedFile current = enumerator.Current;
                    builder.AppendLine(string.Concat(new object[] { "files[\"", current.Name, "\"] = ", GetArrayOfByteArrays(current) }));
                    builder.AppendLine(string.Concat(new object[] { "fileSizes[\"", current.Name, "\"] = ", current.FileSize }));
                }
            }
            finally
            {
                if (enumerator == null)
                {
                }
                enumerator.Dispose();
            }
            builder.Append("}");
            return builder.ToString();
        }

        private static string GetFieldDeclarationForFile(string filepath, string file)
        {
            string[] textArray1 = new string[] { "        [Embed(\"", filepath, "/", file, "_txt\", mimeType=\"application/octet-stream\")]\n        private var ", GetTypeNameForFile(file), ":Class;\n\n" };
            return string.Concat(textArray1);
        }

        private static string GetHead()
        {
            return "\r\npackage\r\n{\r\n        import flash.utils.ByteArray;\r\n        import flash.utils.Dictionary;\r\n        import flash.utils.Endian;\r\n        import UnityEngine.*;\r\n\t\timport com.unity.SerializedFileContainer;\r\n\r\n        public class ProjectSerializedFileContainer extends SerializedFileContainer\r\n        {\r\n                \r\n";
        }

        private static string GetTail()
        {
            return "\t}\n}\n";
        }

        private static string GetTypeNameForFile(string file)
        {
            file = file.Replace("/", "_");
            file = file.Replace(" ", "_");
            return file.Replace(".", "_");
        }

        public static void Write(string filepath, IEnumerable<SerializedFile> files, string outputdir)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(GetHead());
            filepath = Environment.CurrentDirectory + "/" + filepath;
            filepath = filepath.Replace(@"\", "/");
            IEnumerator<SerializedFile> enumerator = files.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    SerializedFile current = enumerator.Current;
                    builder.Append(GetFieldDeclarationForFile(filepath, current.FileName));
                }
            }
            finally
            {
                if (enumerator == null)
                {
                }
                enumerator.Dispose();
            }
            builder.Append(GetConstructor(files));
            builder.Append(GetTail());
            File.WriteAllText(Path.Combine(outputdir, "ProjectSerializedFileContainer.as"), builder.ToString());
        }

        internal class SerializedFile
        {
            public string FileName;
            public long FileSize;
            public string Name;
        }
    }
}

